package com.colin.library.listener;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.colin.library.help.LogHelp;

/**
 * Created by Administrator on 2017/9/15.
 */

public abstract class OnRecyclerLoadMoreListener extends RecyclerView.OnScrollListener {
    private int[] lastPositions;
    private int lastVisibleItemPosition;
    private boolean loadMore = false;

    //对外暴露 加载完之后 设置false
    public void setLoadMore(boolean loadMore) {
        this.loadMore = loadMore;
    }

    public boolean isLoadMore() {
        return loadMore;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
        } else if (layoutManager instanceof GridLayoutManager) {
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
        } else if (layoutManager instanceof LinearLayoutManager) {
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        } else {
            throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState != RecyclerView.SCROLL_STATE_IDLE) {
            return;
        }
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        int visibleItemCount = layoutManager.getChildCount();
        int totalItemCount = layoutManager.getItemCount();
        LogHelp.e("visibleItemCount--------->>" + String.valueOf(visibleItemCount));
        LogHelp.e("totalItemCount----------->>" + String.valueOf(totalItemCount));
        LogHelp.e("lastVisibleItemPosition-->>" + String.valueOf(lastVisibleItemPosition));
        LogHelp.e("getChildCount------------>>" + String.valueOf(recyclerView.getChildCount()));
        if (visibleItemCount > 0 && lastVisibleItemPosition >= totalItemCount - 1 && !isLoadMore()) {
            setLoadMore(true);
            loadMore();
        }
    }

    private int findMax(int[] lastPositions) {
        int max = lastPositions[0];
        for (int value : lastPositions) {
            if (value > max) {
                max = value;
            }
        }
        return max;
    }

    public abstract void loadMore();
}
